using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;



namespace Animation_Editor
{
    public partial class CustomControl1 : ScrollableControl
    {


        public storage data = new storage();
        public Point mouseloc = new Point();

        public int selectedframe;


        

        private int toolstate;
        private Point topleft = new Point(0, 0);
        private Point bottomRight = new Point(0, 0);

        public int Toolstate
        {
            get { return toolstate; }
            set { toolstate = value; }
        }

        public bool rectfinish;
        public bool collfinish;
        public int collnumber;

        public Rectangle collrect = new Rectangle();

        Bitmap spriteSheet = null;
        public Bitmap SpriteSheet
        {
            get { return spriteSheet; }
            set 
            {
                spriteSheet = value;
                SetScrolling();
                Invalidate();
            }
        }

        public CustomControl1()
        {
            InitializeComponent();
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetScrolling();
            rectfinish = false;
            data.LoadedFrame1.LCollisionRects = new List<Rectangle>();
            collnumber = 0;
            selectedframe = -1;

        }

        protected override void OnPaint(PaintEventArgs pe)
        {
           if (spriteSheet != null)
            {

                Point tileSetLocation = new Point(0, 0);
                tileSetLocation.X += this.AutoScrollPosition.X;
                tileSetLocation.Y += this.AutoScrollPosition.Y;

                Rectangle FrameRect = data.LoadedFrame1.RFrameRect;

                FrameRect.X += this.AutoScrollPosition.X;
                FrameRect.Y += this.AutoScrollPosition.Y;

                Point Anchorpt = data.LoadedFrame1.PtAnchorPoint;

                Anchorpt.X += this.AutoScrollPosition.X;
                Anchorpt.Y += this.AutoScrollPosition.Y;
                
                if (rectfinish == true && toolstate == 1)
                {
                    if (mouseloc.X > FrameRect.X)
                    {
                        FrameRect.Width = mouseloc.X - FrameRect.X;
                    }
                    else
                    {
                        FrameRect.Width = FrameRect.X - mouseloc.X;
                        FrameRect.X = mouseloc.X;                        
                    }

                    if (mouseloc.Y > FrameRect.Y)
                    {
                        FrameRect.Height = mouseloc.Y - FrameRect.Y;
                    }
                    else
                    {
                        FrameRect.Height = FrameRect.Y - mouseloc.Y;
                        FrameRect.Y = mouseloc.Y;
                    }
                }

                if (collfinish == true && toolstate == 3)
                {
                    if (mouseloc.X > collrect.X)
                    {
                        collrect.Width = mouseloc.X - collrect.X;
                    }
                    else
                    {
                        collrect.Width = collrect.X - mouseloc.X;
                        collrect.X = mouseloc.X;
                    }

                    if (mouseloc.Y > collrect.Y)
                    {
                        collrect.Height = mouseloc.Y - collrect.Y;
                    }
                    else
                    {
                        collrect.Height = collrect.Y - mouseloc.Y;
                        collrect.Y = mouseloc.Y;
                    }
                }

                            


                pe.Graphics.DrawImage(SpriteSheet, tileSetLocation);
                pe.Graphics.DrawRectangle(Pens.Red, FrameRect);
                if (selectedframe > -1)
                {
                    pe.Graphics.FillEllipse(Brushes.Green, data.LoadedAnimation1.LFrames[selectedframe].PtAnchorPoint.X - 4, data.LoadedAnimation1.LFrames[selectedframe].PtAnchorPoint.Y - 4, 8, 8);
                    pe.Graphics.DrawRectangle(Pens.DarkRed, data.LoadedAnimation1.LFrames[selectedframe].RFrameRect);

                    for (int i = 0; i < data.LoadedAnimation1.LFrames[selectedframe].LCollisionRects.Count ; i++)
                    {
                        pe.Graphics.DrawRectangle(Pens.DarkBlue, data.LoadedAnimation1.LFrames[selectedframe].LCollisionRects[i]);
                    }
                }
                pe.Graphics.FillEllipse(Brushes.Lime, Anchorpt.X - 4, Anchorpt.Y - 4, 8, 8);
                if (data.LoadedFrame1.LCollisionRects.Count > 0)
                {
                    for (int i = 0; i < data.LoadedFrame1.LCollisionRects.Count; i++)
                    {
                        pe.Graphics.DrawRectangle(Pens.Blue, data.LoadedFrame1.LCollisionRects[i]);
                    }
                }
                pe.Graphics.DrawRectangle(Pens.Blue, collrect);
            }

     

            // Calling the base class OnPaint
            
            base.OnPaint(pe);
        }

        private void SetScrolling()
        {
            if (spriteSheet != null)
            {
                this.AutoScrollMinSize = new Size(spriteSheet.Width,spriteSheet.Height);
                Invalidate();
            }
        }

        private void CustomControl1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (toolstate == 0)
                    return;
                if (toolstate == 1)
                {
                    if (rectfinish == false)
                    {
                        topleft.X = e.X - this.AutoScrollPosition.X;
                        topleft.Y = e.Y - this.AutoScrollPosition.Y;
                        rectfinish = true;
                        Rectangle temp = new Rectangle();

                        temp.X = topleft.X;
                        temp.Y = topleft.Y;


                        temp.Height = 0;
                        temp.Width = 0;

                        data.LoadedFrame1.RFrameRect = temp;

                        Invalidate();
                        return;
                        
                    }
                    if (rectfinish == true)
                    {
                        Rectangle temp = new Rectangle();

                        if(topleft.X > e.X)
                        {
                            temp.X = e.X - this.AutoScrollPosition.X;
                            bottomRight.X = topleft.X;

                           
                        }
                        else
                        {
                            temp.X = topleft.X;
                            bottomRight.X = e.X - this.AutoScrollPosition.X;
                                                    
                        }
                           
                        
                        temp.Width = bottomRight.X - temp.X;


                        if (topleft.Y > e.Y)
                        {
                            temp.Y = e.Y - this.AutoScrollPosition.Y;
                            bottomRight.Y = topleft.Y;
                        }
                        else
                        {
                            temp.Y = topleft.Y;
                            bottomRight.Y = e.Y - this.AutoScrollPosition.Y;

                        }

                        temp.Height = bottomRight.Y - temp.Y;

                        rectfinish = false; 

                        data.LoadedFrame1.RFrameRect = temp;
                        
                        Invalidate();
                        return;
                    }

                }
                if (toolstate == 2)
                {
                    Point temppoint = new Point();
                    temppoint.X = e.X - this.AutoScrollPosition.X;
                    temppoint.Y = e.Y - this.AutoScrollPosition.Y;

                    data.LoadedFrame1.PtAnchorPoint = temppoint;
                    Invalidate();              
                    

                }
                if (toolstate == 3)
                {
                    if (collfinish == false)
                    {
                        topleft.X = e.X - this.AutoScrollPosition.X;
                        topleft.Y = e.Y - this.AutoScrollPosition.Y;
                        collfinish = true;
                        Rectangle temp = new Rectangle();

                        temp.X = topleft.X;
                        temp.Y = topleft.Y;


                        temp.Height = 0;
                        temp.Width = 0;

                        collrect = temp;

                        Invalidate();
                        return;
                        
                    }
                    if (collfinish == true)
                    {
                        Rectangle temp = new Rectangle();

                        if (topleft.X > e.X)
                        {
                            temp.X = e.X - this.AutoScrollPosition.X;
                            bottomRight.X = topleft.X;
                        }
                        else
                        {
                            temp.X = topleft.X;
                            bottomRight.X = e.X - this.AutoScrollPosition.X;
                        }


                        temp.Width = bottomRight.X - temp.X;


                        if (topleft.Y > e.Y)
                        {
                            temp.Y = e.Y - this.AutoScrollPosition.Y;
                            bottomRight.Y = topleft.Y;
                        }
                        else
                        {
                            temp.Y = topleft.Y;
                            bottomRight.Y = e.Y - this.AutoScrollPosition.Y;
                        }

                        temp.Height = bottomRight.Y - temp.Y;

                        collfinish = false;

                        collrect = temp;
                        Invalidate();
                        return;
                    }

                }
            }
            //base.OnMouseClick(e);
        }

        private void CustomControl1_MouseMove(object sender, MouseEventArgs e)
        {
            if (rectfinish == true || collfinish == true)
            {
                mouseloc.X = e.X - this.AutoScrollPosition.X;
                mouseloc.Y = e.Y - this.AutoScrollPosition.Y;
                Invalidate();
            }
           
        }

    }
}
